package com.whut.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.whut.mapper.*;
import com.whut.model.*;
import com.whut.service.GeoService;
import com.whut.v_model.CTASummary;
import com.whut.v_model.GeoInfo;
import com.whut.v_model.TimesStat;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service("geoService")
public class GeoServiceImpl implements GeoService {

    @Resource
    private ContracttaskprojectMapper contracttaskprojectMapper;
    @Resource
    private ContractchecktaskMapper contractchecktaskMapper;
    @Resource
    private ContractinfoMapper contractinfoMapper;
    @Resource
    private ProblemcollectMapper problemcollectMapper;
    @Resource
    private GroupmemberMapper groupmemberMapper;
    @Resource
    private ContracttaskacceptMapper contracttaskacceptMapper;


    /*将得到的地图点按照隐患等级分类*/
    private Object classification(List<ContractTaskProject> list){
        JSONObject jsonObject = new JSONObject();
        JSONArray array1 = new JSONArray();
        JSONArray array2 = new JSONArray();
        JSONArray array3 = new JSONArray();
        JSONArray array4 = new JSONArray();
        for (ContractTaskProject project:list) {
            String rl = project.getLatestRiskLevel();
            if(rl.equals("提醒")||rl.equals("1")){
                array1.add(project);
            }else if (rl.equals("2")){
                array2.add(project);
            }else if (rl.equals("3")){
                array3.add(project);
            }else if (rl.equals("4")){
                array4.add(project);
            }
        }
        jsonObject.put("array1",array1);
        jsonObject.put("array2",array2);
        jsonObject.put("array3",array3);
        jsonObject.put("array4",array4);
        return jsonObject;
    }

    @Override
    public Object getAllPoint(int contractId) {
        List<ContractTaskProject> list = contracttaskprojectMapper.getAllPoint(contractId);
        return classification(list);
    }

    @Override
    public Object getProjectNamesByValue(String value,int contractId) {
        List<Contracttaskaccept> projectNameByValue = contracttaskprojectMapper.getProjectNameByValue(value,contractId);
        return projectNameByValue;
    }

    @Override
    public Object getProjectsByContractId(int contractId) {
        List<ContractTaskProject> list = contracttaskprojectMapper.getListByContractId(contractId);
        return classification(list);
    }

    @Override
    public Object getContractList() {
        return contractinfoMapper.getContractUnfinished();
    }

    @Override
    public Object getStatByContractId(int contractId) {
        return problemcollectMapper.getStaticsByContractId(contractId);
    }

    @Override
    public Boolean getIsNeedGeo(int checkId) {
        ContractTaskProject contracttaskproject = contracttaskprojectMapper.getByCCTMkey(checkId);
        if(contracttaskproject.getLongitude()==null||contracttaskproject.getLongitude().equals("")){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public Boolean getIsHeader(int checkId, int userId) {
        ContractCheckTask contractchecktask = contractchecktaskMapper.selectByPrimaryKey(checkId);
        Integer groupId = Integer.parseInt(contractchecktask.getCheckGroupId());
        List<Groupmember> list = groupmemberMapper.getIsHeaderNumber(userId,groupId);
        if(list.size()==0){
            return false;
        }else{
            return true;
        }
    }

    @Override
    public String updateGeoByProjectId(int projectId, float longitude, float latitude) {
        ContractTaskProject contracttaskproject = new ContractTaskProject();
        SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss z");
        Date date1 = new Date(System.currentTimeMillis());
        contracttaskproject.setLastEditTime(formatter.format(date1));
        contracttaskproject.setProjectId(projectId);
        contracttaskproject.setLongitude((double) longitude);
        contracttaskproject.setLatitude((double) latitude);
        try{
            contracttaskprojectMapper.updateByPrimaryKeySelective(contracttaskproject);
            return "succ";
        }catch(Exception e){
            e.printStackTrace();
            return "error";
        }
    }

    @Override
    public String getGeoByProjectId(int projectId) {
        ContractTaskProject projectInfoByProjectId = contracttaskprojectMapper.getProjectInfoByProjectId(projectId);
        Double latitude = projectInfoByProjectId.getLatitude();
        Double longitude = projectInfoByProjectId.getLongitude();
        if (latitude!=null && longitude!=null){   //存在地理位置的话，那么就不用来更新这个地理位置了
            return "true";
        }else{
            return "false";
        }

    }

    @Override
    public Object getProjectDetail(int projectId,int type) {
        JSONObject jsonObject = new JSONObject();
        ContractTaskProject ctp = contracttaskprojectMapper.selectByPrimaryKey(projectId);
        TimesStat timesStat = problemcollectMapper.getStatByProjectAndTime(projectId,"2000-01-01","3000-12-31");
        List<TimesStat> list = problemcollectMapper.getTop3StatByProjectIdAndType(projectId,type);
        //计算出需要增加多少空数据
        int toAddTimes = 3-list.size();
        for(int i = 0 ;i<toAddTimes;i++){
            list.add(new TimesStat(0,0,0,0,0));
        }
        jsonObject.put("ctp",ctp);
        jsonObject.put("timesStat",timesStat);
        jsonObject.put("curAndLast",list);//这个list长度可能为1或者2，如果是2，是按时间倒序排序的
        return jsonObject;
    }


    @Override
    public Object getRecentlyTop3CheckCompareDetail(int projectId, int type) {
        List<TimesStat> list = problemcollectMapper.getTop3StatByProjectIdAndType(projectId,type);
        //计算出需要增加多少空数据
        int toAddTimes = 3-list.size();
        for(int i = 0 ;i<toAddTimes;i++){
            list.add(new TimesStat(0,0,0,0,0));
        }
        return list;
    }

    @Override
    public List<Contracttaskaccept> getCTAByContractId(int contractId) {
        return contracttaskacceptMapper.selectAllByContractId(contractId);
    }

    @Override
    public List<CTASummary> getCTACompareInfo(List<Integer> list,int type) {
        List<CTASummary> res = problemcollectMapper.getCTASummaryStat(list, type);
        return  res;
    }

    @Override
    public Object getRangedTime(int projectId, String st, String et) {
        return problemcollectMapper.getStatByProjectAndTime(projectId,st,et);
    }

    @Override
    public  Object getalluncompletedposition() {
        GeoInfo geoInfo = new GeoInfo();
        String finishState = geoInfo.getFinishState();
        geoInfo.setFinishState(finishState);
        String latestRiskLevel = geoInfo.getLatestRiskLevel();
        geoInfo.setLatestRiskLevel(latestRiskLevel);
        String projectName = geoInfo.getProjectName();
        geoInfo.setProjectName(projectName);
        float longitude = geoInfo.getLongitude();
        geoInfo.setLongitude(longitude);
        float latitude = geoInfo.getLatitude();
        geoInfo.setLatitude(latitude);
        List<GeoInfo> list = contracttaskprojectMapper.getuncompletedPosition();
        return list;
    }
    @Override
    public  Object getallcompletedposition() {
        List<GeoInfo> list = contracttaskprojectMapper.getPosition();
        return list;
    }
    @Override
    public Object getpositionbycontract(int contractId){
        List<GeoInfo> list = contracttaskprojectMapper.getPositionByContract(contractId);
        return list;
    }
    @Override
    public Object getpositionbyid(int projectId){
        List<GeoInfo> list = contracttaskprojectMapper.getPositionById(projectId);
        return list;
    }

    @Override
    public Contractinfo getContractInfoById(int contractId) {
        return contractinfoMapper.selectByPrimaryKey(contractId);
    }

    @Override
    public Object getAllUnFinishedProjectId() {
        List<ContractTaskProject> list=contracttaskprojectMapper.getAllUnfinishedInfo();
        return list;
    }


    /*根据contractId存入委托方地理坐标contractGeo
     * wxd
     * */
    @Override
    public String updateContractGeoByContractId(int contractId, String longitude, String latitude) {
        Contractinfo contractinfo = new Contractinfo();
        SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss z");
        Date date = new Date(System.currentTimeMillis());
        contractinfo.setLastEditTime(formatter.format(date));
        contractinfo.setContractId(contractId);
        contractinfo.setLongitude(longitude);
        contractinfo.setLatitude(latitude);
        try {
            contractinfoMapper.updateContractGeoByContractId(contractinfo);
            return  "succ";
        }catch (Exception e){
            e.printStackTrace();
            return "error";
        }
    }

}
